Git Introduction
What is?
It is a files version system, with Git you control all changes in your files. Like a snapshot when you change a file and run the Git.
Git run in local machine, and you have a possibility to use online with Github, it’s good for create an online copy for your projects, shares projects, and working with a team in the same repository.
Moreover, with Git it’s possibly create branches (A copy of all files, similar a staging area). In a branch you edit the files, and the modifications don’t reflect in the main files.
How to install?
It’s simple and fast to install Git, under you see the steps to each OS.
Linux
If you use Linux, it’s simple to install, only run a command in terminal.
Fedora and similar versions:
sudo yum install git-all
Debian and similar versions:
sudo apt-get install git-all
Mac Os
To install Git in Mac Os just run a command in terminal.
git
If you don’t have Git installed, the system asking to you if you want install.
Windows
To install Git in Windows you need download the installer in the under link.
After to download, you just follow the instructions in the installer to finish install.
Set Up
After to install Git, you need set up your name and email, because those infos are used when saved changed files.
Just run the under commands in terminal:
git config --global user.name "Your Name"
git config --global user.email youremail@example.com
To check your information's, run:
git config --list
First Steps
You use Git in specific directory, and you have three possibilities:
- If you don’t have a directory yet, you just create a folder to project, open terminal inside the directory and run this command to create a Git repository.
git init
- Now if you already have a folder with files of your project and you want start use Git, follow those steps:
Open the terminal in the project directory and run to under command to create a Git repository:
git init
After, run this command to add all already existent files in staging area:
git add .
And to finalized, run the under command to commit all files from staging area to main area.
git commit -m 'initial project version'
- But, if you need download a project from Github, you run the under command:
git clone https://github.com/projectexample
This command above downloaded the directory to local machine, create a Git repository and add all files to main area.
Git Terminology
Under has a list with the principal terms that Git users frequently use.
Working tree: The directory who contains files and sub directories of your project.
Repository: The directory, in the top level of a working tree, where Git keeps all the history and metadata for a project (normally the repository name is .git and it stay hidden).
Hash: A number produced by a hash function that represents the contents of a file or another object as a fixed number of digits.
Object: Inside the Git repo contains four types of objects, each one identifcated by a hash SHA-1:
- A blob object contains a normal file.
- A tree object represents a directory.
- A commit object represents a specific version of the work tree.
- A tag is a name attached to a commit.
Branch: It's a ramification of the working tree, the default branch, which is created when you initialize a repository, is called main, often named master in Git. Example (If you need create a new functionality to your project, you create a branch and all changes you make in the new branch not impact the main branch, only if you merged the branches)
Basic Git Commands
git status
This command show the changes between working tree and the staging area.
git add . #All files
git add example.html #Just one file
The add command is used to Git add in staging area the changes in all files in directory, or if you need controlling just a one file it's possible too.
git commit -m "New script"
This above command save the changes that are in the staging area to the current branch.